/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principal.java
 *
 * Created on May 27, 2010, 4:18:58 PM
 */
package adcon.apresentacao;

import adcon.negocio.condominio.Condominio;
import adcon.persistencia.DAO;
import avant.view.AvantViewUtil;
import avant.view.model.AvantComboModel;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;

/**
 *
 * @author thiagocifani
 */
public class Principal extends javax.swing.JFrame {

    private TelaListagemCondominios telaListagemCondominios;
    private TelaListagemCondominos telaListagemCondominos;
    private TelaCadastroCondominios telaCadastroCondominios;
    public static AvantComboModel<Condominio> ModeloComboCondominio;
    public static List<Condominio> condominios;
    private Condominio condominio;
    private TelaListagemAdvogados telaListagemAdvogados;

    /** Creates new form Principal */
    public Principal() {

        initComponents();
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.setVisible(true);
        adicionarOuvintes();
        carregarComboCondominio();
    }

    private void adicionarOuvintes() {
        ControladorDeEventos controlador = new ControladorDeEventos();

        menuItemBancos.addActionListener(controlador);
        menuItemCondominios.addActionListener(controlador);
        menuItemAdvogados.addActionListener(controlador);
        btnCondominio.addActionListener(controlador);
        btnCondominos.addActionListener(controlador);
        cbCondominios.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent e) {
                setarCondominio();
                
            }
        });
    }

    private Condominio setarCondominio(){
       condominio = ModeloComboCondominio.getObjetoSelecionado();
       this.setTitle("Adcon - " + condominio.getRazaoSocial() + " -  CNPJ:  " + condominio.getCnpj());
        return condominio;
    }

    private void carregarComboCondominio() {
        try {
            condominios = new DAO().listar(Condominio.class, "CondominioPorAtivo", true);
            ModeloComboCondominio = new AvantComboModel<Condominio>(cbCondominios, condominios);
        } catch (Exception e) {
            e.printStackTrace();
            AvantViewUtil.exibirErro("Não foi possível carregar os Condomínios!", this);
        }
    }

    public static void reloadComboCondominios() {
        condominios = new DAO().listar(Condominio.class, "CondominioPorAtivo", true);
        ModeloComboCondominio.setObjetos(condominios);
    }

    private void exibirBancos() {
        TelaListagemBancos tela = new TelaListagemBancos(this, true);
        tela.setVisible(true);
    }
    private void exibirAdvogados(){
        if (desktop.getIndexOf(telaListagemAdvogados) == -1) {
            try {
                telaListagemAdvogados = new TelaListagemAdvogados(desktop);
            } catch (Exception ex) {
                ex.printStackTrace();
                AvantViewUtil.exibirErro("Não foi possível se comunicar com o Banco de dados!", this);
            }
            desktop.add(telaListagemAdvogados);
            centralizar(telaListagemAdvogados);
            telaListagemAdvogados.setVisible(true);
        } else {
            try {
                telaListagemAdvogados.setSelected(true);
            } catch (Exception e) {
               e.printStackTrace();
            }

        }

    }

    private void exibirCondominio() {
        if (desktop.getIndexOf(telaListagemCondominios) == -1) {
            try {
                telaCadastroCondominios = new TelaCadastroCondominios(condominio, null);
            } catch (Exception ex) {
                ex.printStackTrace();
                AvantViewUtil.exibirErro("Não foi possível se comunicar com o Banco de dados!", this);
            }
            desktop.add(telaCadastroCondominios);
            centralizar(telaCadastroCondominios);
            telaCadastroCondominios.setVisible(true);
        } else {
            try {
                telaCadastroCondominios.setSelected(true);
            } catch (Exception e) {
               e.printStackTrace();
            }

        }
    }

    private void exibirCondominios() {
        if (desktop.getIndexOf(telaListagemCondominios) == -1) {
            try {
                telaListagemCondominios = new TelaListagemCondominios(desktop);
            } catch (Exception ex) {
                ex.printStackTrace();
                AvantViewUtil.exibirErro("Não foi possível se comunicar com o Banco de dados!", this);
            }
            desktop.add(telaListagemCondominios);
            centralizar(telaListagemCondominios);
            telaListagemCondominios.setVisible(true);
        } else {
            try {
                telaListagemCondominios.setSelected(true);
            } catch (Exception e) {
               e.printStackTrace();
            }
        }
    }

    private void exibirCondominos() {
        if (desktop.getIndexOf(telaListagemCondominos) == -1) {
            try {
                telaListagemCondominos = new TelaListagemCondominos(condominio, desktop);
            } catch (Exception ex) {
                ex.printStackTrace();
                AvantViewUtil.exibirErro("Não foi possível se comunicar com o Banco de dados!", this);
            }
            desktop.add(telaListagemCondominos);
            centralizar(telaListagemCondominos);
            telaListagemCondominos.setVisible(true);
        } else {
            try {
                telaListagemCondominos.setSelected(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        desktop = new javax.swing.JDesktopPane();
        jToolBar1 = new javax.swing.JToolBar();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        btnCondominos = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        btnCondominio = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cbCondominios = new javax.swing.JComboBox();
        jMenuBar1 = new javax.swing.JMenuBar();
        menuCadastro = new javax.swing.JMenu();
        menuItemCondominios = new javax.swing.JMenuItem();
        menuItemAdvogados = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        MenuItemContas = new javax.swing.JMenuItem();
        menuItemUsuarios = new javax.swing.JMenuItem();
        menuItemBancos = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem11 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem12 = new javax.swing.JMenuItem();
        jMenuItem13 = new javax.swing.JMenuItem();
        jMenuItem14 = new javax.swing.JMenuItem();
        jMenuItem15 = new javax.swing.JMenuItem();
        jMenuItem16 = new javax.swing.JMenuItem();
        jMenuItem17 = new javax.swing.JMenuItem();
        jMenuItem18 = new javax.swing.JMenuItem();
        jMenuItem19 = new javax.swing.JMenuItem();
        jMenuItem20 = new javax.swing.JMenuItem();
        jMenuItem21 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem22 = new javax.swing.JMenuItem();
        jMenuItem23 = new javax.swing.JMenuItem();
        jMenuItem24 = new javax.swing.JMenuItem();
        jMenuItem25 = new javax.swing.JMenuItem();
        jMenuItem26 = new javax.swing.JMenuItem();
        jMenuItem27 = new javax.swing.JMenuItem();
        jMenuItem28 = new javax.swing.JMenuItem();
        jMenuItem29 = new javax.swing.JMenuItem();
        jMenuItem30 = new javax.swing.JMenuItem();
        jMenu5 = new javax.swing.JMenu();
        jMenu6 = new javax.swing.JMenu();
        jMenuItem31 = new javax.swing.JMenuItem();
        jMenuItem32 = new javax.swing.JMenuItem();
        jMenuItem33 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Adcon");
        setMinimumSize(new java.awt.Dimension(800, 600));
        getContentPane().setLayout(new java.awt.GridBagLayout());

        desktop.setBackground(new java.awt.Color(0, 102, 102));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(desktop, gridBagConstraints);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setPreferredSize(new java.awt.Dimension(10, 50));
        jToolBar1.add(jSeparator2);

        btnCondominos.setFont(new java.awt.Font("Tahoma", 1, 12));
        btnCondominos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/adcon/imagens/ListarCondominios.png"))); // NOI18N
        btnCondominos.setText("Condominos");
        btnCondominos.setFocusable(false);
        btnCondominos.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCondominos.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btnCondominos);
        jToolBar1.add(jSeparator3);

        btnCondominio.setFont(new java.awt.Font("Tahoma", 1, 12));
        btnCondominio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/adcon/imagens/ListarCondominios.png"))); // NOI18N
        btnCondominio.setText("Dados do Condomínio");
        btnCondominio.setFocusable(false);
        btnCondominio.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCondominio.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btnCondominio);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        getContentPane().add(jToolBar1, gridBagConstraints);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setPreferredSize(new java.awt.Dimension(20, 45));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel1.setText("Usuário:");

        jTextField1.setEditable(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Condominio:");

        cbCondominios.setFont(new java.awt.Font("Tahoma", 1, 12));

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 131, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(72, 72, 72)
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(cbCondominios, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 347, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(49, 49, 49))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(14, 14, 14)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2)
                    .add(cbCondominios, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(13, 13, 13))
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        getContentPane().add(jPanel1, gridBagConstraints);

        jMenuBar1.setPreferredSize(new java.awt.Dimension(236, 30));

        menuCadastro.setText("Cadastro");
        menuCadastro.setFont(new java.awt.Font("Tahoma", 0, 12));

        menuItemCondominios.setFont(new java.awt.Font("Tahoma", 1, 12));
        menuItemCondominios.setText("Condomínios");
        menuCadastro.add(menuItemCondominios);

        menuItemAdvogados.setFont(new java.awt.Font("Tahoma", 1, 12));
        menuItemAdvogados.setText("Advogados");
        menuCadastro.add(menuItemAdvogados);
        menuCadastro.add(jSeparator1);

        MenuItemContas.setFont(new java.awt.Font("Tahoma", 1, 12));
        MenuItemContas.setText("Contas");
        menuCadastro.add(MenuItemContas);

        menuItemUsuarios.setFont(new java.awt.Font("Tahoma", 1, 12));
        menuItemUsuarios.setText("Usuários");
        menuCadastro.add(menuItemUsuarios);

        menuItemBancos.setFont(new java.awt.Font("Tahoma", 1, 12));
        menuItemBancos.setText("Bancos");
        menuCadastro.add(menuItemBancos);

        jMenuItem6.setFont(new java.awt.Font("Tahoma", 1, 12));
        jMenuItem6.setText("Multa e Juros");
        menuCadastro.add(jMenuItem6);

        jMenuItem7.setFont(new java.awt.Font("Tahoma", 1, 12));
        jMenuItem7.setText("Indexadores");
        menuCadastro.add(jMenuItem7);

        jMenuItem8.setFont(new java.awt.Font("Tahoma", 1, 12));
        jMenuItem8.setText("Tabela Prolagos");
        menuCadastro.add(jMenuItem8);

        jMenuItem9.setFont(new java.awt.Font("Tahoma", 1, 12));
        jMenuItem9.setText("Indexadores");
        menuCadastro.add(jMenuItem9);

        jMenuItem10.setFont(new java.awt.Font("Tahoma", 1, 12));
        jMenuItem10.setText("Logotipos");
        menuCadastro.add(jMenuItem10);

        jMenuBar1.add(menuCadastro);

        jMenu2.setText("Finanças");
        jMenu2.setFont(new java.awt.Font("Tahoma", 0, 12));

        jMenuItem11.setText("Conta Corrente");
        jMenu2.add(jMenuItem11);

        jMenu3.setText("Extrato Bancário");

        jMenuItem12.setText("Extrato Diário");
        jMenu3.add(jMenuItem12);

        jMenuItem13.setText("Extrato Mensal");
        jMenu3.add(jMenuItem13);

        jMenuItem14.setText("Identificadores");
        jMenu3.add(jMenuItem14);

        jMenu2.add(jMenu3);

        jMenuItem15.setText("Remessa Salário");
        jMenu2.add(jMenuItem15);

        jMenuItem16.setText("Poupança");
        jMenu2.add(jMenuItem16);

        jMenuItem17.setText("Aplicações");
        jMenu2.add(jMenuItem17);

        jMenuItem18.setText("Consignações");
        jMenu2.add(jMenuItem18);

        jMenuItem19.setText("Empréstimos");
        jMenu2.add(jMenuItem19);

        jMenuItem20.setText("Contas Indispensáveis");
        jMenu2.add(jMenuItem20);

        jMenuItem21.setText("Contas Dispensáveis");
        jMenu2.add(jMenuItem21);

        jMenuBar1.add(jMenu2);

        jMenu4.setText("Cobranças");
        jMenu4.setFont(new java.awt.Font("Tahoma", 0, 12));

        jMenuItem22.setText("Lançamentos");
        jMenu4.add(jMenuItem22);

        jMenuItem23.setText("Inadimplência");
        jMenu4.add(jMenuItem23);

        jMenuItem24.setText("Pagos");
        jMenu4.add(jMenuItem24);

        jMenuItem25.setText("Água");
        jMenu4.add(jMenuItem25);

        jMenuItem26.setText("Retorno");
        jMenu4.add(jMenuItem26);

        jMenuItem27.setText("Cobrança Base");
        jMenu4.add(jMenuItem27);

        jMenuItem28.setText("Pgts Morto");
        jMenu4.add(jMenuItem28);

        jMenuItem29.setText("Cálculo da Maior Cota");
        jMenu4.add(jMenuItem29);

        jMenuItem30.setText("Árvore de Cobranças");
        jMenu4.add(jMenuItem30);

        jMenuBar1.add(jMenu4);

        jMenu5.setText("Relatórios");
        jMenu5.setFont(new java.awt.Font("Tahoma", 0, 12));

        jMenu6.setText("Cadastro");

        jMenuItem31.setText("condomínios");
        jMenu6.add(jMenuItem31);

        jMenuItem32.setText("condominos");
        jMenu6.add(jMenuItem32);

        jMenuItem33.setText("contas");
        jMenu6.add(jMenuItem33);

        jMenu5.add(jMenu6);

        jMenuBar1.add(jMenu5);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }

    public static void centralizar(JInternalFrame fr) {
        Dimension d = fr.getDesktopPane().getSize();
        fr.setLocation((d.width - fr.getSize().width) / 2, (d.height - fr.getSize().height) / 2);
    }

    private class ControladorDeEventos implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            Object origem = e.getSource();

            if (origem == btnCondominio) {
                if (condominio != null) {
                    exibirCondominio();
                } else {
                    AvantViewUtil.exibirAdvertencia("Selecione um Condomínio", null);
                }
            } else if (origem == menuItemBancos) {
                exibirBancos();
            } else if (origem == menuItemCondominios) {
                exibirCondominios();
            } else if (origem == btnCondominos) {
                if (condominio != null) {
                    exibirCondominos();
                } else {
                    AvantViewUtil.exibirAdvertencia("Selecione um Condomínio", null);
                }
            }else if (origem == menuItemAdvogados){
                exibirAdvogados();
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem MenuItemContas;
    private javax.swing.JButton btnCondominio;
    private javax.swing.JButton btnCondominos;
    private javax.swing.JComboBox cbCondominios;
    private javax.swing.JDesktopPane desktop;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem13;
    private javax.swing.JMenuItem jMenuItem14;
    private javax.swing.JMenuItem jMenuItem15;
    private javax.swing.JMenuItem jMenuItem16;
    private javax.swing.JMenuItem jMenuItem17;
    private javax.swing.JMenuItem jMenuItem18;
    private javax.swing.JMenuItem jMenuItem19;
    private javax.swing.JMenuItem jMenuItem20;
    private javax.swing.JMenuItem jMenuItem21;
    private javax.swing.JMenuItem jMenuItem22;
    private javax.swing.JMenuItem jMenuItem23;
    private javax.swing.JMenuItem jMenuItem24;
    private javax.swing.JMenuItem jMenuItem25;
    private javax.swing.JMenuItem jMenuItem26;
    private javax.swing.JMenuItem jMenuItem27;
    private javax.swing.JMenuItem jMenuItem28;
    private javax.swing.JMenuItem jMenuItem29;
    private javax.swing.JMenuItem jMenuItem30;
    private javax.swing.JMenuItem jMenuItem31;
    private javax.swing.JMenuItem jMenuItem32;
    private javax.swing.JMenuItem jMenuItem33;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JMenu menuCadastro;
    private javax.swing.JMenuItem menuItemAdvogados;
    private javax.swing.JMenuItem menuItemBancos;
    private javax.swing.JMenuItem menuItemCondominios;
    private javax.swing.JMenuItem menuItemUsuarios;
    // End of variables declaration//GEN-END:variables
}
